1 00:00:00,580 --> 00:00:05,470 We're going to continue where we left off on types by learning how to create our own custom types. 2 00:00:05,470 --> 00:00:10,360 Creating our own types can be useful for several reasons, but one reason is that creating custom types 3 00:00:10,360 --> 00:00:14,380 also allows for us to create a default type that is needed for some kind of system. 4 00:00:14,380 --> 00:00:19,960 For example, maybe we have a custom crafting or inventory system that requires each crafting system 5 00:00:19,960 --> 00:00:21,610 to be set up in a particular way. 6 00:00:21,610 --> 00:00:25,510 We can achieve and enforce a particular setup using a custom type. 7 00:00:25,510 --> 00:00:30,370 Custom types are also incredibly useful for object oriented programming, where we have a class and 8 00:00:30,370 --> 00:00:35,380 enforce the return of a constructor to produce a variable of a specific custom type, and we can also 9 00:00:35,380 --> 00:00:38,470 create custom types for the parameter section of a constructor. 10 00:00:38,470 --> 00:00:43,240 So someone knows exactly what needs to be fulfilled and what is optional to give to the constructor. 11 00:00:43,240 --> 00:00:47,680 You'll see us use custom types later in the course, but just know that Roblox gives us the ability 12 00:00:47,680 --> 00:00:50,440 to do this, even if it seems a little useless at the moment. 13 00:00:50,710 --> 00:00:55,990 So to create a custom type, we can use the keyword type, and then afterwards we can just pass the 14 00:00:55,990 --> 00:00:57,520 name like we would for a variable. 15 00:00:57,520 --> 00:00:59,830 So I'll call this one example table. 16 00:01:01,280 --> 00:01:03,860 And then we can set it equal to an empty table. 17 00:01:03,860 --> 00:01:07,250 And then we could also pass some parameters in here that this type may have. 18 00:01:07,250 --> 00:01:10,340 So we could do example param. 19 00:01:10,340 --> 00:01:12,590 And this parameter needs to be a number. 20 00:01:13,190 --> 00:01:18,680 So now we have this custom type here that any variable we set to have this type must be a table. 21 00:01:18,680 --> 00:01:22,970 And it has to contain this example parameter in there with a number attached to it. 22 00:01:23,530 --> 00:01:29,530 So if I create a new variable like some table and I set it to be of the type example table, as you 23 00:01:29,530 --> 00:01:35,320 can see now, it appears when we go out and look for types, and then we can set it equal to a new table. 24 00:01:35,320 --> 00:01:37,480 And then we can create the example parameter. 25 00:01:37,480 --> 00:01:38,860 As you can see it shows up right here. 26 00:01:38,860 --> 00:01:43,060 And it tells us it needs to be a number because we have set it to be of the type example table. 27 00:01:43,060 --> 00:01:46,000 So we can go ahead and fulfill this just like that. 28 00:01:46,330 --> 00:01:47,410 Now the table is stored at. 29 00:01:47,410 --> 00:01:54,040 This variable is considered sealed, meaning that you can't add or you cannot remove any parameters 30 00:01:54,040 --> 00:01:55,000 from this table. 31 00:01:55,000 --> 00:02:00,610 As an example, if I index some table and try to add another parameter like, we'll call it another 32 00:02:00,610 --> 00:02:01,420 param. 33 00:02:02,680 --> 00:02:05,770 And we set it equal to a number like three. 34 00:02:05,890 --> 00:02:07,900 As you can see, we're going to get an error here. 35 00:02:07,900 --> 00:02:09,280 And it says cannot add property. 36 00:02:09,280 --> 00:02:11,020 Another parameter to example table. 37 00:02:11,020 --> 00:02:13,330 This is because this table is sealed. 38 00:02:13,360 --> 00:02:17,740 Tables are sealed when we explicitly define a type for a table to be. 39 00:02:17,770 --> 00:02:21,550 Tables are unsealed when we just create them like this. 40 00:02:21,550 --> 00:02:23,650 So if I do another table. 41 00:02:24,170 --> 00:02:25,730 And just do that. 42 00:02:25,730 --> 00:02:30,890 This table is considered unsealed and we can add and remove anything we'd like from the table, because 43 00:02:30,890 --> 00:02:33,500 we didn't explicitly define a type for this table. 44 00:02:33,500 --> 00:02:37,520 So this is unsealed and the one above is sealed. 45 00:02:38,310 --> 00:02:42,720 As another example, I'm going to create a type and I'm just going to call it inventory. 46 00:02:43,890 --> 00:02:47,640 And inside of this table I can define some things that an inventory would have. 47 00:02:47,640 --> 00:02:53,190 For example it may contain a table that has, you know, items inside of it. 48 00:02:53,190 --> 00:02:59,490 So what we could do to be able to add multiple things to this items table is that we could define that 49 00:02:59,490 --> 00:03:01,860 the key has to be a number. 50 00:03:02,220 --> 00:03:05,880 And then we can define the value attached to this number is a string. 51 00:03:07,010 --> 00:03:12,680 And then maybe we could add an optional parameter to this table, like the max capacity of the inventory. 52 00:03:12,680 --> 00:03:14,210 And we'll set it to a number. 53 00:03:14,210 --> 00:03:18,440 And then we put that question mark there to make this an optional parameter. 54 00:03:19,150 --> 00:03:22,960 Now when we go to create a new inventory, we could do new inventory. 55 00:03:24,620 --> 00:03:30,440 Declare it of the type inventory and set it equal to a table with some items in it, and just set it 56 00:03:30,440 --> 00:03:32,870 to, I guess, an empty table. 57 00:03:33,440 --> 00:03:39,500 From this point, we can index new inventory and get the items table, and now we can add new index 58 00:03:39,500 --> 00:03:40,220 value pairs. 59 00:03:40,220 --> 00:03:46,370 For example, at this first index I could create something like I don't know, we have a rock right? 60 00:03:47,570 --> 00:03:53,330 And then maybe at the second index we have an item like, I don't know, wood. 61 00:03:53,330 --> 00:03:55,310 And then at the third index. 62 00:03:56,250 --> 00:03:58,860 We have something like a crystal. 63 00:03:59,100 --> 00:04:02,850 And then once we've created these items in this inventory, we can go ahead and print them out in the 64 00:04:02,850 --> 00:04:03,270 console. 65 00:04:03,270 --> 00:04:07,410 So new inventory, we can get the item at the first index. 66 00:04:08,510 --> 00:04:12,620 And then when we go to run our game, we should get rock printed in the console. 67 00:04:12,620 --> 00:04:13,130 And we do. 68 00:04:13,160 --> 00:04:13,970 Perfect. 69 00:04:14,990 --> 00:04:19,340 And of course, one more thing to take a look at is that when we type out new inventory, we can see 70 00:04:19,340 --> 00:04:22,010 it is of our custom type inventory. 71 00:04:22,790 --> 00:04:27,320 So this was just a brief overview of how we can create our own custom types in Roblox. 72 00:04:27,350 --> 00:04:31,700 While it may seem a little useless at first, there are going to be quite useful when we're going to 73 00:04:31,700 --> 00:04:34,310 start using them in module scripts and our classes. 74 00:04:34,340 --> 00:04:36,020 See you in the next lecture.